package com.ruoyi.web.controller.biz;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.BsRelationConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.valid.ValidInsertGroup;
import com.ruoyi.common.valid.ValidUpdateGroup;
import com.ruoyi.system.domain.BsRelation;
import com.ruoyi.system.domain.ro.BsRelationRO;
import com.ruoyi.system.service.IBsRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 关系记录（关注、收藏、点赞）
 *
 * @author ruoyi
 * @date 2023-11-25
 */
@Validated
@RequiredArgsConstructor
@Api(tags = "游客端-点赞、收藏、关注")
@RestController
@RequestMapping("/system/relation")
@Slf4j
public class BsRelationController extends BaseController {

    private final IBsRelationService iBsRelationService;

    //customer_attention 游客关注  strategy_like 攻略点赞 strategy_collect 攻略收藏

    @ApiOperation(value = "游客关注", tags = {"游客端-点赞、收藏、关注"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aimId", value = "目标id:关注或取关用户id 点赞或取消点赞作品id 收藏或取消收藏作品id"),
    })
    @Log(title = "游客关注", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/customerAttention")
    public Result<?> customerAttention(@RequestParam() String aimId) {
        Long userId = SecurityUtils.getUserId();
        BsRelationRO ro = BsRelationRO.builder()
                .createById(SecurityUtils.getUserId())
                .aimId(aimId)
                .type(BsRelationConstants.customer_attention)
                .build();
        if (aimId.equals(String.valueOf(userId))) {
            log.info("登录人关注当前登录人,直接返回:{}", ro);
            return Result.success();
        }
        if (exist(ro)) {
            return Result.success();
        }
        return Result.toResult((iBsRelationService.insertByBo(ro)));
    }

    @ApiOperation(value = "攻略点赞", tags = {"游客端-点赞、收藏、关注"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aimId", value = "目标id:关注或取关用户id 点赞或取消点赞作品id 收藏或取消收藏作品id"),
    })
    @Log(title = "攻略点赞", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/strategyLike")
    public Result<?> strategyLike(@RequestParam() String aimId) {
        BsRelationRO ro = BsRelationRO.builder()
                .createById(SecurityUtils.getUserId())
                .aimId(aimId)
                .type(BsRelationConstants.strategy_like)
                .build();
        if (exist(ro)) {
            return Result.success();
        }
        return Result.toResult((iBsRelationService.insertByBo(ro)));
    }

    @ApiOperation(value = "攻略收藏", tags = {"游客端-点赞、收藏、关注"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aimId", value = "目标id:关注或取关用户id 点赞或取消点赞作品id 收藏或取消收藏作品id"),
    })
    @Log(title = "攻略收藏", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/strategyCollect")
    public Result<?> strategyCollect(@RequestParam() String aimId) {
        BsRelationRO ro = BsRelationRO.builder()
                .createById(SecurityUtils.getUserId())
                .aimId(aimId)
                .type(BsRelationConstants.strategy_collect)
                .build();
        if (exist(ro)) {
            return Result.success();
        }
        return Result.toResult((iBsRelationService.insertByBo(ro)));
    }

    private boolean exist(BsRelationRO ro) {
        boolean b = !iBsRelationService.queryList(ro).isEmpty();
        log.info("关注点赞收藏记录是否存在:{}, 入参:{}", b, JSONObject.toJSONString(ro));
        return b;
    }

    //=======================取消

    @ApiOperation(value = "游客关注-取消", tags = {"游客端-点赞、收藏、关注"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aimId", value = "目标id:关注或取关用户id 点赞或取消点赞作品id 收藏或取消收藏作品id"),
    })
    @Log(title = "游客关注-取消", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/customerAttentionCancel")
    public Result<?> customerAttentionCancel(@RequestParam() String aimId) {
        BsRelationRO ro = BsRelationRO.builder()
                .createById(SecurityUtils.getUserId())
                .aimId(aimId)
                .type(BsRelationConstants.customer_attention)
                .build();
        return Result.toResult((iBsRelationService.deleteByParam(ro)));
    }

    @ApiOperation(value = "攻略点赞-取消", tags = {"游客端-点赞、收藏、关注"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aimId", value = "目标id:关注或取关用户id 点赞或取消点赞作品id 收藏或取消收藏作品id"),
    })
    @Log(title = "攻略点赞-取消", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/strategyLikeCancel")
    public Result<?> strategyLikeCancel(@RequestParam() String aimId) {
        BsRelationRO ro = BsRelationRO.builder()
                .createById(SecurityUtils.getUserId())
                .aimId(aimId)
                .type(BsRelationConstants.strategy_like)
                .build();
        return Result.toResult((iBsRelationService.deleteByParam(ro)));
    }

    @ApiOperation(value = "攻略收藏-取消", tags = {"游客端-点赞、收藏、关注"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aimId", value = "目标id:关注或取关用户id 点赞或取消点赞作品id 收藏或取消收藏作品id"),
    })
    @Log(title = "攻略收藏-取消", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/strategyCollectCancel")
    public Result<?> strategyCollectCancel(@RequestParam() String aimId) {
        BsRelationRO ro = BsRelationRO.builder()
                .createById(SecurityUtils.getUserId())
                .aimId(aimId)
                .type(BsRelationConstants.strategy_collect)
                .build();
        return Result.toResult((iBsRelationService.deleteByParam(ro)));
    }


    /**
     * 查询关系记录（关注、收藏、点赞）列表
     */
    @GetMapping("/list")
    public Result<TableDataInfo<BsRelation>> list(BsRelationRO bo) {
        startPage();
        List<BsRelation> list = iBsRelationService.queryPageList(bo);
        return Result.success(getDataTable(list));
    }
}
